package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 侧边栏菜单项DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "侧边栏菜单项信息")
public class SidebarItemDTO {

    @NotNull(message = "菜单项ID不能为空")
    @Positive(message = "菜单项ID必须是正整数")
    @Schema(description = "菜单项唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer id;

    @NotBlank(message = "菜单项名称不能为空")
    @Schema(description = "菜单项显示名称", example = "教学资源", requiredMode = Schema.RequiredMode.REQUIRED)
    private String name;

    @Schema(description = "菜单项图标", example = "document")
    private String icon;

    @Schema(description = "菜单项路由路径", example = "/resources")
    private String route;

    @Schema(description = "权限标识", example = "resource:read")
    private String permission;

    @Schema(description = "是否展开（用于有子菜单的情况）", example = "false")
    @Builder.Default
    private Boolean expanded = false;

    @Schema(description = "排序顺序", example = "1")
    @Builder.Default
    private Integer sortOrder = 0;

    @Schema(description = "是否启用", example = "true")
    @Builder.Default
    private Boolean enabled = true;

    @Schema(description = "子菜单项列表")
    private List<SubItemDTO> children;

    /**
     * 获取完整的菜单路径
     */
    @Schema(description = "完整菜单路径", accessMode = Schema.AccessMode.READ_ONLY)
    public String getFullPath() {
        return route != null ? route : "/" + name.toLowerCase().replace(" ", "-");
    }

    /**
     * 判断是否有子菜单
     */
    @Schema(description = "是否有子菜单", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean hasChildren() {
        return children != null && !children.isEmpty();
    }

    /**
     * 获取子菜单数量
     */
    @Schema(description = "子菜单数量", accessMode = Schema.AccessMode.READ_ONLY)
    public int getChildCount() {
        return hasChildren() ? children.size() : 0;
    }

    /**
     * 验证菜单项是否有效
     */
    @Schema(description = "菜单项是否有效", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isValid() {
        return id != null && name != null && !name.trim().isEmpty();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "子菜单项信息")
    public static class SubItemDTO {

        @NotNull(message = "子菜单项ID不能为空")
        @Positive(message = "子菜单项ID必须是正整数")
        @Schema(description = "子菜单项唯一标识", example = "101", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer id;

        @NotBlank(message = "子菜单项名称不能为空")
        @Schema(description = "子菜单项显示名称", example = "课件管理", requiredMode = Schema.RequiredMode.REQUIRED)
        private String name;

        @Schema(description = "子菜单项图标", example = "folder")
        private String icon;

        @Schema(description = "子菜单项路由路径", example = "/resources/courses")
        private String route;

        @Schema(description = "权限标识", example = "course:read")
        private String permission;

        @Schema(description = "排序顺序", example = "1")
        @Builder.Default
        private Integer sortOrder = 0;

        @Schema(description = "是否启用", example = "true")
        @Builder.Default
        private Boolean enabled = true;

        /**
         * 获取完整的子菜单路径
         */
        @Schema(description = "完整子菜单路径", accessMode = Schema.AccessMode.READ_ONLY)
        public String getFullPath() {
            return route != null ? route : "/" + name.toLowerCase().replace(" ", "-");
        }

        /**
         * 验证子菜单项是否有效
         */
        @Schema(description = "子菜单项是否有效", accessMode = Schema.AccessMode.READ_ONLY)
        public boolean isValid() {
            return id != null && name != null && !name.trim().isEmpty();
        }
    }
}
